/**
 * @author Sebastian
 * @version 2.0
 * @date 2024/5/21 9:35
 */
// 找最长的回文子串
    // 连续的最长的回文子串
// 双指针解法
    //从中间开始向两边扩散判断
public class Solution5 {
    public String longestPalindrome(String s) {
        String result = "";
        int len = s.length();
        for (int i = 0; i < len; i++) {
            String s1 = palindrome(s , i, i);
            String s2 = palindrome(s, i, i + 1);
            result = result.length() > s1.length() ? result : s1;
            result = result.length() > s2.length() ? result : s2;
        }
        return result;
    }

    private String palindrome(String s, int l, int r) {
        while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
            l--;
            r++;
        }
        //返回的字符串是从beginIndex（包含）到endIndex（不包含）的子串
        //不应该是substring(l + 1, r - 1);
        return s.substring(l + 1, r);
    }
}
